﻿
//Banner V1.0
var images = [];
var fadeSpeed = 500;
var changeBannerSpeed = 5000;
var xmlObj;
var bannerIndex;
var bannerCNT;
var TimerId;
var isClick = false;
// Preload a list of images with an optional callback as a final parameter.
function preload() {
    if (arguments.length == 0) return;
    var waiting = arguments.length - 1,
count = 0,
callback = arguments[arguments.length - 1];
    if (typeof callback == "string") {
        callback = function () { };
        waiting++;
    }
    function loaded() {
        if (++count == waiting) callback();
    }
    for (var i = 0; i < waiting; i++) {
        var image = new Image();
        image.onload = loaded;
        image.src = arguments[i];
        images.push(image);
    }
}
function indexBanners(xml) {
    xmlObj = xml;
    bannerCNT = $("banner", xmlObj).length;
    if (bannerCNT > 6) bannerCNT = 6;
    getSBanner();
    $(document).ready(function () {
        $("#banner_go_left").hide();
        $("#banner_go_right").hide();
        $("#banner_go_left a").click(getPeriousBanner);
        $("#banner_go_right a").click(getNextBanner);
        if (bannerCNT > 1) {
            $("#banner").mousemove(function (e) {
                var pos;
                if (e.pageX < $(this).offset().left + ($(this).width() / 2)) {
                    $("#banner_go_left").show();
                    $("#banner_go_right").hide();
                }
                else {
                    $("#banner_go_right").show();
                    $("#banner_go_left").hide();
                }
            });
            $("#banner").mouseleave(function () {
                $("#banner_go_right").hide();
                $("#banner_go_left").hide();
            });
        }
    });
}
function getSBanner() {
    $("banner", xmlObj).each(function (index) {
        if ($("banner", xmlObj).index($(this)) < bannerCNT) {
            $("#selector_container").append("<a href='#'><img src='images/hover_point.png' alt='' /></a>");
        }
    });
    $("#selector_container a").click(function (e) {
        if ($(".animated").html() == null) {
            home_banner_small_cell_click($(this));
        }
        e.preventDefault();
    });
    resetA();
    bannerIndex = 0;
    preload($("banner:first image", xmlObj).text(), function () {
        $("#banner_link").append("<img src='" + $("banner:first image", xmlObj).text() + "' border='0' style='display: none'/>");
        $("#selector_container a:first").click();
        getBigBanner();
    });
}
function getBigBanner() {
    var indexNow = $("#banner_link img").index($("#banner_link img:last")[0]);
    if (indexNow <= bannerCNT) {
        preload($("banner:eq(" + indexNow + ") image", xmlObj).text(), function () {
            $("#banner_link").append("<img src='" + $("banner:eq(" + indexNow + ") image", xmlObj).text() + "' border='0' style='display: none'/>");
            getBigBanner();
        });
    }
}
function bannerFade(targetIndex, orgIndex) {
    $("#banner_link img:eq(" + targetIndex + ")").fadeIn(fadeSpeed, function () {
        $("#banner_link").attr("href", $("banner:eq(" + (targetIndex - 1) + ") link", xmlObj).text());
        $("#banner_link").attr("target", $("banner:eq(" + (targetIndex - 1) + ") link_target", xmlObj).text());
        $("#banner_link").attr("alt", $("banner:eq(" + (targetIndex - 1) + ") wording", xmlObj).text());
        bannerIndex = targetIndex;
        TimerId = setInterval("getNextBanner()", changeBannerSpeed);
        $("#banner").removeClass("animated");
    });
    $("#banner_link img:eq(" + orgIndex + ")").fadeOut(fadeSpeed);
}
function home_banner_small_cell_click(obj) {
    var objIndex = $("#selector_container a").index($(obj)[0]);
    //check is click the banner showing now
    if (objIndex != bannerIndex - 1) {
        //check big banner loading
        if ($("#banner_link img:eq(" + (objIndex + 1) + ")").html() == null) {
            home_banner_small_cell_click(obj);
            return false;
        }
        $("#banner").addClass("animated");
        clearTimeout(TimerId);
        bannerFade($("#selector_container a").index($(obj)[0]) + 1, bannerIndex);
        $("#selector_container img").attr({ src: "images/hover_point.png" });
        $("img", obj).attr({ src: "images/active_point.png" });
        $("#selector_container a").removeClass("currnt_image");
        $(obj).addClass("currnt_image");
    }
}
function resetA() {
    $("a").focus(function () {
        $(this).blur();
    });
}
function getNextBanner() {
    if (bannerIndex < bannerCNT)
        $("#selector_container a:eq(" + (bannerIndex) + ")").click();
    else
        $("#selector_container a:eq(0)").click();
    return false;
}
function getPeriousBanner() {
    if (bannerIndex - 1 > 0)
        $("#selector_container a:eq(" + (bannerIndex - 2) + ")").click();
    else
        $("#selector_container a:eq(" + (bannerCNT - 1) + ")").click();
    return false;
}
